using System;
using System.ComponentModel;

namespace MT5Trade.Models
{
    /// <summary>
    /// 组合盈亏策略配置
    /// </summary>
    public class CombinedPnLSettings
    {
        /// <summary>
        /// 目标净盈利金额（美元）
        /// </summary>
        [DefaultValue(100.0)]
        public double TargetNetProfit { get; set; }

        /// <summary>
        /// 安全系数（用于覆盖执行延迟，默认1.05）
        /// </summary>
        [DefaultValue(1.05)]
        public double SafetyFactor { get; set; }

        /// <summary>
        /// 检查间隔（毫秒）
        /// </summary>
        [DefaultValue(100)]
        public int CheckIntervalMs { get; set; }

        /// <summary>
        /// 最大时间差（毫秒），用于原子性约束
        /// </summary>
        [DefaultValue(500)]
        public int MaxTimeDifferenceMs { get; set; }

        /// <summary>
        /// 最小持仓时间（秒），避免高频交易
        /// </summary>
        [DefaultValue(30)]
        public int MinHoldingTimeSeconds { get; set; }

        /// <summary>
        /// 最大重试次数
        /// </summary>
        [DefaultValue(3)]
        public int MaxRetryCount { get; set; }

        /// <summary>
        /// 平仓超时时间（毫秒）
        /// </summary>
        [DefaultValue(2000)]
        public int CloseTimeoutMs { get; set; }

        /// <summary>
        /// 时间窗口（秒），用于识别对冲仓位
        /// </summary>
        [DefaultValue(5.0)]
        public double TimeWindowSeconds { get; set; }

        /// <summary>
        /// 是否启用组合盈亏模式
        /// </summary>
        public bool IsEnabled { get; set; }

        /// <summary>
        /// 是否记录详细日志
        /// </summary>
        [DefaultValue(true)]
        public bool EnableDetailedLogging { get; set; }

        /// <summary>
        /// 默认滑点估计（点数）
        /// </summary>
        [DefaultValue(1.0)]
        public double DefaultSlippage { get; set; }

        /// <summary>
        /// 最大允许仓位对数量
        /// </summary>
        [DefaultValue(100)]
        public int MaxPairCount { get; set; }

        /// <summary>
        /// 紧急停止标志
        /// </summary>
        public bool EmergencyStop { get; set; }

        /// <summary>
        /// 最大亏损限制（美元），作为安全防线
        /// </summary>
        [DefaultValue(-1000.0)]
        public double MaxLossLimit { get; set; }

        /// <summary>
        /// 构造函数，初始化默认值
        /// </summary>
        public CombinedPnLSettings()
        {
            TargetNetProfit = 100.0;
            SafetyFactor = 1.05;
            CheckIntervalMs = 100;
            MaxTimeDifferenceMs = 500;
            MinHoldingTimeSeconds = 30;
            MaxRetryCount = 3;
            CloseTimeoutMs = 2000;
            TimeWindowSeconds = 5.0;
            IsEnabled = false;
            EnableDetailedLogging = true;
            DefaultSlippage = 1.0;
            MaxPairCount = 100;
            EmergencyStop = false;
            MaxLossLimit = -1000.0;
        }

        /// <summary>
        /// 验证配置有效性
        /// </summary>
        public bool ValidateSettings(out string errorMessage)
        {
            errorMessage = string.Empty;

            if (TargetNetProfit <= 0)
            {
                errorMessage = "目标净盈利必须大于0";
                return false;
            }

            if (SafetyFactor < 1.0 || SafetyFactor > 2.0)
            {
                errorMessage = "安全系数必须在1.0到2.0之间";
                return false;
            }

            if (CheckIntervalMs < 50 || CheckIntervalMs > 5000)
            {
                errorMessage = "检查间隔必须在50ms到5000ms之间";
                return false;
            }

            if (MaxTimeDifferenceMs < 100 || MaxTimeDifferenceMs > 2000)
            {
                errorMessage = "最大时间差必须在100ms到2000ms之间";
                return false;
            }

            if (MinHoldingTimeSeconds < 0)
            {
                errorMessage = "最小持仓时间不能为负";
                return false;
            }

            if (MaxRetryCount < 0 || MaxRetryCount > 10)
            {
                errorMessage = "最大重试次数必须在0到10之间";
                return false;
            }

            if (CloseTimeoutMs < 500 || CloseTimeoutMs > 10000)
            {
                errorMessage = "平仓超时时间必须在500ms到10000ms之间";
                return false;
            }

            if (TimeWindowSeconds < 1.0 || TimeWindowSeconds > 60.0)
            {
                errorMessage = "时间窗口必须在1秒到60秒之间";
                return false;
            }

            if (MaxPairCount < 1 || MaxPairCount > 1000)
            {
                errorMessage = "最大仓位对数量必须在1到1000之间";
                return false;
            }

            return true;
        }

        /// <summary>
        /// 复制当前配置
        /// </summary>
        public CombinedPnLSettings Clone()
        {
            return new CombinedPnLSettings
            {
                TargetNetProfit = this.TargetNetProfit,
                SafetyFactor = this.SafetyFactor,
                CheckIntervalMs = this.CheckIntervalMs,
                MaxTimeDifferenceMs = this.MaxTimeDifferenceMs,
                MinHoldingTimeSeconds = this.MinHoldingTimeSeconds,
                MaxRetryCount = this.MaxRetryCount,
                CloseTimeoutMs = this.CloseTimeoutMs,
                TimeWindowSeconds = this.TimeWindowSeconds,
                IsEnabled = this.IsEnabled,
                EnableDetailedLogging = this.EnableDetailedLogging,
                DefaultSlippage = this.DefaultSlippage,
                MaxPairCount = this.MaxPairCount,
                EmergencyStop = this.EmergencyStop,
                MaxLossLimit = this.MaxLossLimit
            };
        }

        public override string ToString()
        {
            return $"CombinedPnLSettings: Target={TargetNetProfit}, Safety={SafetyFactor}, Interval={CheckIntervalMs}ms, Enabled={IsEnabled}";
        }
    }
}